/**                                                                                                                                                                                
 * Copyright (c) 2010 Yahoo! Inc. All rights reserved.                                                                                                                             
 *                                                                                                                                                                                 
 * Licensed under the Apache License, Version 2.0 (the "License"); you                                                                                                             
 * may not use this file except in compliance with the License. You                                                                                                                
 * may obtain a copy of the License at                                                                                                                                             
 *                                                                                                                                                                                 
 * http://www.apache.org/licenses/LICENSE-2.0                                                                                                                                      
 *                                                                                                                                                                                 
 * Unless required by applicable law or agreed to in writing, software                                                                                                             
 * distributed under the License is distributed on an "AS IS" BASIS,                                                                                                               
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or                                                                                                                 
 * implied. See the License for the specific language governing                                                                                                                    
 * permissions and limitations under the License. See accompanying                                                                                                                 
 * LICENSE file.                                                                                                                                                                   
 */

package tlsb;

import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;

import measurements.Measurements;

/**
 * Wrapper around a "real" DB that measures latencies and counts return codes.
 */
public class DBWrapper extends DB
{
	DB _db;
	Measurements _measurements;

	public DBWrapper(DB db)
	{
		_db=db;
		_measurements=Measurements.getMeasurements();
	}

	/**
	 * Set the properties for this DB.
	 */
	public void setProperties(Properties p)
	{
		_db.setProperties(p);
	}

	/**
	 * Get the set of properties for this DB.
	 */
	public Properties getProperties()
	{
		return _db.getProperties();
	}

	/**
	 * Initialize any state for this DB.
	 * Called once per DB instance; there is one DB instance per client thread.
	 */
	public void init() throws DBException
	{
		_db.init();
	}

	/**
	 * Cleanup any state for this DB.
	 * Called once per DB instance; there is one DB instance per client thread.
	 */
	public void cleanup() throws DBException
	{
		_db.cleanup();
	}

	
	public int follow(String uid, String fid)
	{
		long st=System.currentTimeMillis();
		int res=_db.follow(uid,fid);
		long en=System.currentTimeMillis();
		_measurements.measure("FOLLOW",(int)(en-st));
		_measurements.reportReturnCode("FOLLOW",res);
		return res;
	}
	
	public int unfollow(String uid, String fid)
	{
		long st=System.currentTimeMillis();
		int res=_db.unfollow(uid,fid);
		long en=System.currentTimeMillis();
		_measurements.measure("UNFOLLOW",(int)(en-st));
		_measurements.reportReturnCode("UNFOLLOW",res);
		return res;
	}
	
	public int newUser()
	{
		long st=System.currentTimeMillis();
		int res=_db.newUser();
		long en=System.currentTimeMillis();
		_measurements.measure("NEWUSER",(int)(en-st));
		_measurements.reportReturnCode("NEWUSER",res);
		return res;
	}
	public int post(String uid,String mid)
	{
		long st=System.currentTimeMillis();
		int res=_db.post(uid,mid);
		long en=System.currentTimeMillis();
		if(en-st > 100000){
			_measurements.measure("POST",10);
			_measurements.reportReturnCode("POST",res);
			return res;
		}
		_measurements.measure("POST",(int)(en-st));
		_measurements.reportReturnCode("POST",res);
		return res;
	}
//	public int getByPush(String uid)
//	{
//		long st=System.currentTimeMillis();
//		int res=_db.getByPush(uid);
//		long en=System.currentTimeMillis();
//		_measurements.measure("PUSH",(int)(en-st));
//		_measurements.reportReturnCode("PUSH",res);
//		return res;
//	}
//	public int getByPull(String uid)
//	{
//		long st=System.currentTimeMillis();
//		int res=_db.getByPull(uid);
//		long en=System.currentTimeMillis();
//		_measurements.measure("PULL",(int)(en-st));
//		_measurements.reportReturnCode("PULL",res);
//		return res;
//	}
//	public int getByHybrid(String uid)
//	{
//		long st=System.currentTimeMillis();
//		int res=_db.getByHybrid(uid);
//		long en=System.currentTimeMillis();
//		_measurements.measure("HYBRID",(int)(en-st));
//		_measurements.reportReturnCode("HYBRID",res);
//		return res;
//	}
	public int getHTL(String uid,int start)
	{
		long st=System.currentTimeMillis();
		int res=_db.getHTL(uid,start);
		long en=System.currentTimeMillis();
		_measurements.measure("GETHTL",(int)(en-st));
		_measurements.reportReturnCode("GETHTL",res);
		return res;
	}
}
